package com.company.nuwa.common.utils;

/**
 * <p>说明：经纬度处理</p >
 *
 * @author youyoukele
 * @version 1.0
 * @date 2022/12/27 上午11:38
 */
public class MapDistanceUtils {

    private static final double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 根据两个位置的经纬度，来计算两地的距离（单位为KM）
     *
     * @param lon1 用户经度
     * @param lat1 用户纬度
     * @param lon2 商家经度
     * @param lat2 商家纬度
     * @return
     */
    public static Double getDistance(Double lon1, Double lat1, Double lon2, Double lat2) {
        double patm = 2;
        double radLon1 = rad(lon1);
        double radLon2 = rad(lon2);
        double difference = radLon1 - radLon2;
        double mdifference = rad(lat1) - rad(lat2);
        double distance = patm * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / patm), patm)
                + Math.cos(radLon1) * Math.cos(radLon2)
                * Math.pow(Math.sin(mdifference / patm), patm)));
        distance = distance * EARTH_RADIUS;
        return distance;
    }

    public static void main(String[] args) {
        // 济南国际会展中心经纬度：117.11811  36.68484
        // 趵突泉：117.00999000000002  36.66123
        System.out.println(getDistance(117.12404270324492, 36.691530586173606, 121.29187302117616, 31.098330905228156));
    }

}